package team.kirohuji.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import team.kirohuji.domain.system.UrlFilter;
import team.kirohuji.repository.jpa.system.UrlFilterRepository;
import team.kirohuji.service.system.UrlFilterService;

@RestController
@RequestMapping("/url")
public class UrlFilterController {
	@Resource
	private UrlFilterService urlFilterService;

	@Autowired
	private UrlFilterRepository urlFilterRepository;
	/**
	 * Saves a given entity. Use the returned instance for further operations as the
	 * save operation might have changed the entity instance completely.
	 * 
	 * @param entity
	 * @return the saved entity
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	UrlFilter save(@RequestBody UrlFilter entity) {
		return urlFilterService.createUrlFilter(entity);
	}
	
	/**
	 * Returns the number of entities available.
	 * 
	 * @return the number of entities
	 */
	@RequestMapping(value = "/count", method = RequestMethod.GET)
	long count() {
		return urlFilterRepository.count();
	}

	/**
	 * Deletes the entity with the given id.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @throws IllegalArgumentException
	 *             in case the given {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	void delete(@PathVariable Integer id) {
		urlFilterService.deleteUrlFilter(id);
	}

	/**
	 * Retrieves an entity by its id.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @return the entity with the given id or {@literal null} if none found
	 * @throws IllegalArgumentException
	 *             if {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	UrlFilter findOne(@PathVariable Integer id) {
		return urlFilterRepository.getOne(id);
	}

	/**
	 * Deletes all entities managed by the urlFilterRepository. 根据repository 删除所有的记录
	 */
	@RequestMapping(value = "/all", method = RequestMethod.DELETE)
	void deleteAll() {
		urlFilterRepository.deleteAll();
	}

	/**
	 * Saves all given entities.
	 * 
	 * @param entities
	 * @return the saved entities
	 * @throws IllegalArgumentException
	 *             in case the given entity is (@literal null}.
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	List<UrlFilter> save(@RequestBody List<UrlFilter> entities) {
		entities.forEach(u->{
			urlFilterService.createUrlFilter(u);
		});
		return entities;
	}

	/**
	 * Returns all instances of the type.
	 * 
	 * @return all entities
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	List<UrlFilter> findAll() {
		return urlFilterRepository.findAll();
	}

	/**
	 * Returns whether an entity with the given id exists.
	 * 
	 * @param id
	 *            must not be {@literal null}.
	 * @return true if an entity with the given id exists, {@literal false}
	 *         otherwise
	 * @throws IllegalArgumentException
	 *             if {@code id} is {@literal null}
	 */
	@RequestMapping(value = "/exists/{id}", method = RequestMethod.GET)
	boolean exists(@PathVariable Integer id) {
		return urlFilterRepository.existsById(id);
	}

	/**
	 * Returns all instances of the type with the given IDs.
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/g/{ids}", method = RequestMethod.GET)
	List<UrlFilter> findAll(@PathVariable String ids) {
		return urlFilterRepository.findAllById(
				Arrays.asList(ids.split("-")).stream().map(Integer::parseInt).collect(Collectors.toList()));
	}

	/**
	 * Returns a {@link Page} of entities meeting the paging restriction provided in
	 * the {@code Pageable} object.
	 * 
	 * 分页获取数据
	 * 
	 * @param page
	 *            第几页
	 * @param limit
	 *            每页最长记录数
	 * @return a page of entities
	 */
	@RequestMapping(value = "/page", method = RequestMethod.GET)
	Page<UrlFilter> findAll(@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "limit", defaultValue = "5") Integer limit) {
		Sort sort = new Sort(Direction.DESC, "urlFilterId");
		Pageable pageable = new PageRequest(page, limit, sort);
		return urlFilterRepository.findAll(pageable);
	}

	/**
	 * Flushes all pending changes to the database.
	 */
	@RequestMapping(value = "/flush", method = RequestMethod.GET)
	void flush() {
		urlFilterRepository.flush();
	}
}
